//
//  GoodsFlashSaleHeadView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/13.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "GoodsFlashSaleHeadView.h"
#import "TimeClockView.h"

@interface GoodsFlashSaleHeadView ()

@property (nonatomic, strong) UIView *priceView;

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *oldPriceTitleLabel;
@property (nonatomic, strong) UILabel *oldPriceLabel;
@property (nonatomic, strong) UILabel *saleCountLabel;

@property (nonatomic, strong) UIView *timeView;

@property (nonatomic, strong) UILabel *timeTitleLabel;
@property (nonatomic, strong) TimeClockView *clockView;

@end

@implementation GoodsFlashSaleHeadView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configureUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.priceLabel.frame = CGRectMake(10, 0, self.priceLabel.width, self.height);
    self.oldPriceTitleLabel.frame = CGRectMake(self.priceLabel.right + 5, self.height * 0.5 - 15, 22 , 15);
    self.oldPriceLabel.frame = CGRectMake(self.oldPriceTitleLabel.right, self.oldPriceTitleLabel.origin.y, self.width - 115 - self.oldPriceTitleLabel.right, 15);
    
    self.saleCountLabel.frame = CGRectMake(self.priceLabel.right + 5, self.height * 0.5, self.width - 115 - self.saleCountLabel.left, 15);
    
    self.timeTitleLabel.frame = CGRectMake(0, 0, 115, self.height * 0.5);
    
    self.clockView.frame = CGRectMake((115 - 11 * 6) * 0.5, self.height * 0.5, 11 * 6, 18);
    
    self.timeView.frame = CGRectMake(self.width - 115, 0, 115, self.height);
    self.priceView.frame = CGRectMake(0, 0, self.width - 115, self.height);
    [self.priceView setGradientHorizontal:@[[UIColor colorWithHex:0xF22E53 alpha:1.0],[UIColor colorWithHex:0xF22D90 alpha:1.0]]];
}

#pragma mark - Method
- (void)configureUI {
    [self.priceView addSubview:self.priceLabel];
    [self.priceView addSubview:self.oldPriceTitleLabel];
    [self.priceView addSubview:self.oldPriceLabel];
    [self.priceView addSubview:self.saleCountLabel];
    
    [self.timeView addSubview:self.timeTitleLabel];
    [self.timeView addSubview:self.clockView];
    
    [self addSubview:self.priceView];
    [self addSubview:self.timeView];
}

- (void)setValueWithPrice:(NSString *)price oldPrice:(NSString *)oldPrice saleCount:(NSString *)saleCount time:(CGFloat)time {
    
    NSMutableAttributedString *priceAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",price] attributes:@{NSFontAttributeName:TextPingFangMediumFontSize(14)}];
    [priceAttStr setAttributes:@{NSFontAttributeName:TextPingFangMediumFontSize(28)} range:NSMakeRange(1, price.length)];
    self.priceLabel.attributedText = priceAttStr;
    [self.priceLabel sizeToFit];
    self.priceLabel.height = self.height;
//    NSDictionary *attributes = @{NSFontAttributeName:self.priceLabel.font};
//    CGFloat length = [self.priceLabel.text boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
//    self.priceLabel.width = length;

    NSMutableAttributedString *oldPriceAttStr = [[NSMutableAttributedString alloc] initWithString:oldPrice];
    [oldPriceAttStr addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
                                   NSStrikethroughColorAttributeName:[UIColor whiteColor]}
                           range:NSMakeRange(0, oldPrice.length)];
    [self.oldPriceLabel setAttributedText:oldPriceAttStr];
    
    self.oldPriceTitleLabel.text = @"原价";
    self.saleCountLabel.text = [NSString stringWithFormat:@"销量%@笔", saleCount];
    [self.clockView setTimerForReset:time];
    self.timeTitleLabel.text = @"距离结束还剩：";
    
    [self layoutSubviews];
}

#pragma mark - Getters
- (UIView *)priceView {
    if (!_priceView) {
        _priceView = [[UIView alloc] init];
    }
    return _priceView;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = TextPingFangFontSize(20);
        _priceLabel.textColor = [UIColor whiteColor];
    }
    return _priceLabel;
}

- (UILabel *)oldPriceTitleLabel {
    if (!_oldPriceTitleLabel) {
        _oldPriceTitleLabel = [[UILabel alloc] init];
        _oldPriceTitleLabel.font = TextPingFangFontSize(10);
        _oldPriceTitleLabel.textColor = [UIColor whiteColor];
    }
    return _oldPriceTitleLabel;
}

- (UILabel *)oldPriceLabel {
    if (!_oldPriceLabel) {
        _oldPriceLabel = [[UILabel alloc] init];
        _oldPriceLabel.font = TextPingFangFontSize(10);
        _oldPriceLabel.textColor = [UIColor whiteColor];
    }
    return _oldPriceLabel;
}

- (UILabel *)saleCountLabel {
    if (!_saleCountLabel) {
        _saleCountLabel = [[UILabel alloc] init];
        _saleCountLabel.font = TextPingFangFontSize(10);
        _saleCountLabel.textColor = [UIColor whiteColor];
    }
    return _saleCountLabel;
}

- (UIView *)timeView {
    if (!_timeView) {
        _timeView = [[UIView alloc] init];
        _timeView.backgroundColor = [UIColor colorWithHex:0xFFEAE9 alpha:1.0];
    }
    return _timeView;
}

- (UILabel *)timeTitleLabel {
    if (!_timeTitleLabel) {
        _timeTitleLabel = [[UILabel alloc] init];
        _timeTitleLabel.font = TextPingFangFontSize(10);
        _timeTitleLabel.textColor = [UIColor colorWithHex:0xF2315F alpha:1.0];
        _timeTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _timeTitleLabel;
}

- (TimeClockView *)clockView {
    if (!_clockView) {
        _clockView = [[TimeClockView alloc] initWithCornerRadius:4.0 andColor:[UIColor colorWithHex:0xF2315F alpha:1.0]];
    }
    return _clockView;
}

@end
